import 'package:flutter/material.dart';
import '../../models/serach_info_model.dart';
import '../view/product_common_view.dart';
import 'package:ninetyfour/l10n/app_localizations.dart';
import '../../manger/network_utils.dart';

class CommentPage extends StatefulWidget {
  final String categoryId;

  const CommentPage({Key? key, required this.categoryId}) : super(key: key);

  @override
  _CommentPageState createState() => _CommentPageState();
}

class _CommentPageState extends State<CommentPage> {
  bool _isLoading = true;
  List<CommentListModel> _comments = [];
  String _errorMessage = '';

  @override
  void initState() {
    super.initState();
    _loadComments();
  }

  Future<void> _loadComments() async {
    try {
      final response = await NetworkUtils.get(
        RequestCmd.commentPage,
        params: {"categoryId": widget.categoryId,"pageSize":99,"pageNum":1},
      );

      final model = CommentPageModel.fromJson(response);
      
      if (model.code == 200) {
        setState(() {
          _comments = model.data?.page?.records ?? [];
          _isLoading = false;
        });
      } else {
        setState(() {
          _errorMessage = '加载评论失败';
          _isLoading = false;
        });
      }
    } catch (e) {
      setState(() {
        _errorMessage = '网络连接异常';
        _isLoading = false;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        toolbarHeight: 40,
        title: Text(
          AppLocalizations.of(context)!.evaluate,
          style: TextStyle(fontSize: 17), // 设置字体大小为18
        ),
      ),
      body: ListView.builder(
        padding: EdgeInsets.all(15),
        itemCount: _isLoading ? 1 : _comments.length,
        itemBuilder: (context, index) {
          return Container(
            margin: EdgeInsets.only(bottom: 10),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(12),
            ),
            child: _isLoading
            ? Center(child: CircularProgressIndicator())
            : _errorMessage.isNotEmpty
                ? Center(child: Text(_errorMessage))
                : CommentCell(comment: _comments[index]),
          );
        },
      ),
    );
  }
}